package example1_loginLog.listener;

import example1_loginLog.service.LoginLogService;
import example1_loginLog.vo.LoginLog;
import example1_loginLog.vo.User;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@WebListener
public class LoginLogListener implements HttpSessionAttributeListener {

    //初始化所有已登录用户的信息(用户id,此用户对应的session信息)
    Map<String, HttpSession> userSessionMap = new HashMap<>();

    LoginLogService logService = new LoginLogService();

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {

        HttpSession session = sbe.getSession();
        String attrName = sbe.getName();

        //如果session添加的属性为 user的话表示是登录成功，需要写日志
        if(attrName.equals("user")){


            String userid = ((User)session.getAttribute("user")).getId();

            //如果Map中包含当前用户的登录信息，则获取之前的session并手动清除(踢掉原登录信息)
            if(userSessionMap.containsKey(userid)){
                userSessionMap.get(userid).invalidate();
            }

            //然后把此用户的新登录信息更新到这个Map中
            userSessionMap.put(userid,session);

            //创建日志的实体类对象
            LoginLog log = new LoginLog(
               UUID.randomUUID().toString().toUpperCase(),
               userid,
               "登录",
               LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
               session.getId()
            );

            //把这个登录信息写入数据库
            int logCount = logService.writeLog(log);

            if(logCount == 1){
                System.out.println("登录日志写入成功");
            }else{
                System.out.println("登录日志写入失败");
            }

        }

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {

        HttpSession session = sbe.getSession();
        String attrName = sbe.getName();

        //如果session移除的属性为 user的话表示是退出登录，需要写日志
        if(attrName.equals("user")){

            String userid = (String) session.getAttribute("userid");
            //如果此用户已经退出登录了，map中的数据也要清除掉
            HttpSession userSession = userSessionMap.get(userid);
            userSession.invalidate();
            userSessionMap.remove(userid);

            //创建日志的实体类对象
            LoginLog log = new LoginLog(
                    UUID.randomUUID().toString().toUpperCase(),
                    (String) session.getAttribute("userid"),
                    "退出登录",
                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                    session.getId()
            );

            System.out.println(log);

            //把这个登录信息写入数据库
            int logCount = logService.writeLog(log);

            if(logCount == 1){
                System.out.println("退出登录日志写入成功");
            }else{
                System.out.println("退出登录日志写入失败");
            }

        }

    }

}
